﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using System.Collections;
namespace Exact_Extension
{
    public partial class InvoiceFullfillmentOTHF : DevExpress.XtraEditors.XtraForm
    {
        Common common;
        ExactDataContext DS;
        List<ISHCMC_V_StudentListWithDebtor> ListOfStudent;
        XMLExport xmlExport;
        public InvoiceFullfillmentOTHF(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            common = new Common(DS);
            xmlExport = new XMLExport(DS);

        }
        private Hashtable GetHashPaidAmount()
        {
            var queryPaid = from ListPaid in DS.ISHCMC_V_TuitionFeePayment_CheckFufills
                            select ListPaid;
            Hashtable hashData = new Hashtable();
            foreach (ISHCMC_V_TuitionFeePayment_CheckFufill itemFF in queryPaid)
            {
                string key = itemFF.debnr.Trim() + " " + itemFF.docnumber;
                if (hashData.ContainsKey(key))
                {
                    if (hashData[key] != null)
                    {
                        hashData[key] = Convert.ToDouble(hashData[key]) + itemFF.PaidAmount;
                    }

                }
                else
                {
                    hashData[key] = itemFF.PaidAmount;
                }
            }
            return hashData;
        }
        private void ProcessListInvoice(ISHCMC_V_Invoice[] ListInvoiceQuery)
        {
            List<ISHCMC_V_Invoice> ListInvoice = new List<ISHCMC_V_Invoice>();
            Hashtable hashData = this.GetHashPaidAmount();
            Hashtable hashInvoice = new Hashtable();

            foreach (ISHCMC_V_Invoice itemInvoice in ListInvoiceQuery)
            {
                string itemCode = "";
                if (itemInvoice.Itemcode != "APPF" && itemInvoice.Itemcode != "ADF")
                {
                    itemCode = "OTHER";
                }
                else
                {
                    itemCode = itemInvoice.Itemcode;
                }
                if (hashInvoice.ContainsKey(itemCode))
                {
                    List<ISHCMC_V_Invoice> ListInvLocal = (List<ISHCMC_V_Invoice>)hashInvoice[itemCode];
                    ListInvLocal.Add(itemInvoice);
                    hashInvoice[itemCode] = ListInvLocal;
                }
                else
                {
                    List<ISHCMC_V_Invoice> ListInvLocal = new List<ISHCMC_V_Invoice>();
                    ListInvLocal.Add(itemInvoice);
                    hashInvoice[itemCode] = ListInvLocal;
                }
            }
            List<ISHCMC_V_Invoice> ListInvAPPF = (List<ISHCMC_V_Invoice>)hashInvoice["APPF"];
            if (ListInvAPPF != null)
            {
                foreach (ISHCMC_V_Invoice itemInvAPPF in ListInvAPPF)
                {
                    string key = itemInvAPPF.Debnr ;
                    double paidamount = 0;
                    double netamount = 0;
                    if (itemInvAPPF.NetAmount.HasValue)
                    {
                        netamount = Convert.ToDouble(itemInvAPPF.NetAmount.Value);
                    }
                    if (hashData.ContainsKey(key))
                    {
                        if (hashData[key] != null)
                        {
                            paidamount = Convert.ToDouble(hashData[key]);

                            if (paidamount <= netamount)
                            {
                                itemInvAPPF.PaidAmount = paidamount;
                                itemInvAPPF.Outstanding = netamount - paidamount;
                                hashData[key] = 0;
                            }
                            else
                            {
                                itemInvAPPF.PaidAmount = netamount;
                                itemInvAPPF.Outstanding = netamount - itemInvAPPF.PaidAmount;
                                hashData[key] = paidamount - netamount;

                            }
                        }
                    }
                    else
                    {
                        itemInvAPPF.Outstanding = netamount - paidamount;
                    }
                    ListInvoice.Add(itemInvAPPF);
                }
            }
            List<ISHCMC_V_Invoice> ListInvADF = (List<ISHCMC_V_Invoice>)hashInvoice["ADF"];
            if (ListInvADF != null)
            {
                foreach (ISHCMC_V_Invoice itemInvADF in ListInvADF)
                {
                    string key = itemInvADF.Debnr ;
                    double paidamount = 0;
                    double netamount = 0;
                    if (itemInvADF.NetAmount.HasValue)
                    {
                        netamount = Convert.ToDouble(itemInvADF.NetAmount.Value);
                    }
                    if (hashData.ContainsKey(key))
                    {
                        if (hashData[key] != null)
                        {
                            paidamount = Convert.ToDouble(hashData[key]);

                            if (paidamount <= netamount)
                            {
                                itemInvADF.PaidAmount = paidamount;
                                itemInvADF.Outstanding = netamount - paidamount;
                                hashData[key] = 0;
                            }
                            else
                            {
                                itemInvADF.PaidAmount = netamount;
                                itemInvADF.Outstanding = netamount - itemInvADF.PaidAmount;
                                hashData[key] = paidamount - netamount;

                            }
                        }
                    }
                    else
                    {
                        itemInvADF.Outstanding = netamount - paidamount;
                    }
                    ListInvoice.Add(itemInvADF);
                }
            }
            List<ISHCMC_V_Invoice> ListInvOTHER = (List<ISHCMC_V_Invoice>)hashInvoice["OTHER"];
            if (ListInvOTHER != null)
            {
                foreach (ISHCMC_V_Invoice itemInvOTHER in ListInvOTHER)
                {
                    string key = itemInvOTHER.Debnr;
                    double paidamount = 0;
                    double netamount = 0;
                    if (itemInvOTHER.NetAmount.HasValue)
                    {
                        netamount = Convert.ToDouble(itemInvOTHER.NetAmount.Value);
                    }
                    if (hashData.ContainsKey(key))
                    {
                        if (hashData[key] != null)
                        {
                            paidamount = Convert.ToDouble(hashData[key]);


                            if (paidamount <= netamount)
                            {
                                itemInvOTHER.PaidAmount = paidamount;
                                itemInvOTHER.Outstanding = netamount - paidamount;
                                hashData[key] = 0;
                            }
                            else
                            {
                                itemInvOTHER.PaidAmount = netamount;
                                itemInvOTHER.Outstanding = netamount - itemInvOTHER.PaidAmount;
                                hashData[key] = paidamount - netamount;

                            }
                        }
                    }
                    else
                    {
                        itemInvOTHER.Outstanding = netamount - paidamount;
                    }
                    ListInvoice.Add(itemInvOTHER);
                }
            }

            this.iSHCMCVInvoiceBindingSource.DataSource = ListInvoice;
        }
        public void LoadInvoice(List<ISHCMC_V_StudentListWithDebtor> ListStudent,string fulfilled)
        {
            List<ISHCMC_V_Invoice> ListInvoice = new List<ISHCMC_V_Invoice>();
            ListOfStudent = ListStudent;
            string[] ListItem = { "ADF", "APPF", "TF", "COE" ,"REENF"};
              List<string> ListStudentID = new List<string>();
            foreach (ISHCMC_V_StudentListWithDebtor itemStudent in ListStudent)
            {
                ListStudentID.Add(itemStudent.StudentID);

            }
                var queryInv = from InvDetail in DS.ISHCMC_V_Invoices
                               
                               where ListStudentID.Contains(InvDetail.StudentID)
                             
                               && InvDetail.Status ==0
                               && InvDetail.Fulfilled == fulfilled
                               && !ListItem.Contains(InvDetail.Itemcode)
                               select InvDetail;
                
                     ProcessListInvoice(queryInv.ToArray());
            this.ShowDialog();
        }
        public void LoadInvoiceWithoutDialog(List<ISHCMC_V_StudentListWithDebtor> ListStudent,string fulfilled)
        {
            List<ISHCMC_V_Invoice> ListInvoice = new List<ISHCMC_V_Invoice>();
            ListOfStudent = ListStudent;
            string[] ListItem = { "ADF", "APPF", "TF", "COE", "REENF" };
            List<string> ListStudentID = new List<string>();
            foreach (ISHCMC_V_StudentListWithDebtor itemStudent in ListStudent)
            {
                ListStudentID.Add(itemStudent.StudentID);

            }
            var queryInv = from InvDetail in DS.ISHCMC_V_Invoices

                           where ListStudentID.Contains(InvDetail.StudentID)

                           && InvDetail.Status == 0
                           && InvDetail.Fulfilled == fulfilled
                           && !ListItem.Contains(InvDetail.Itemcode)
                           select InvDetail;
               ProcessListInvoice(queryInv.ToArray());
           
        }
        public void LoadInvoicePrintedVAT(List<ISHCMC_V_StudentListWithDebtor> ListStudent,string fulfilled)
        {
            List<ISHCMC_V_Invoice> ListInvoice = new List<ISHCMC_V_Invoice>();
            ListOfStudent = ListStudent;

            var queryExactInvoice = from ExactInvoice in DS.ISHCMC_V_ExactInvoices
                                    select ExactInvoice;
            string[] ListItem = { "ADF", "APPF", "TF", "COE", "REENF" };
          
            List<string> ListStudentID = new List<string>();
            foreach (ISHCMC_V_StudentListWithDebtor itemStudent in ListStudent)
            {
                ListStudentID.Add(itemStudent.StudentID);

            }
                var queryInv = from InvDetail in DS.ISHCMC_V_Invoices
                               join TaxInvTrack in DS.ISHCMC_TaxInvoiceTrackings
                               on InvDetail.ProformaInvoiceNo equals TaxInvTrack.InvoiceNo
                               where ListStudentID.Contains(InvDetail.StudentID)
                            
                               && InvDetail.Status == 0  && TaxInvTrack.Status != 1
                               && InvDetail.StudentID == TaxInvTrack.StudentID
                               && InvDetail.Fulfilled == fulfilled
                                && !ListItem.Contains(InvDetail.Itemcode)
                               select InvDetail;
                
                      ProcessListInvoice(queryInv.ToArray());
        }


        public void LoadInvoiceDetail(List<ISHCMC_V_StudentListWithDebtor> ListStudent,List<ISHCMC_Invoice> ListInvoiceDetail, string fulfilled)
        {
            List<ISHCMC_V_Invoice> ListInvoice = new List<ISHCMC_V_Invoice>();
            ListOfStudent = ListStudent;
            string[] ListItem = { "ADF", "APPF", "TF", "COE", "REENF" };
            List<string> ListStudentID = new List<string>();
            List<int> ListInvID = new List<int>();
            foreach (ISHCMC_V_StudentListWithDebtor itemStudent in ListStudent)
            {
                ListStudentID.Add(itemStudent.StudentID);

            }
            foreach (ISHCMC_Invoice itemInv in ListInvoiceDetail)
            {
                ListInvID.Add(itemInv.ID);
            }
            var queryInv = from InvDetail in DS.ISHCMC_V_Invoices

                           where ListStudentID.Contains(InvDetail.StudentID)

                           && InvDetail.Status == 0
                           && InvDetail.Fulfilled == fulfilled
                           && !ListItem.Contains(InvDetail.Itemcode)
                           && ListInvID.Contains(InvDetail.ID)
                           select InvDetail;

                 ProcessListInvoice(queryInv.ToArray());
            this.ShowDialog();
        }

        public void LoadInvoiceDetailWithoutDialog(List<ISHCMC_V_StudentListWithDebtor> ListStudent, List<ISHCMC_V_Invoice> ListInvoiceDetail, string fulfilled)
        {
            List<ISHCMC_V_Invoice> ListInvoice = new List<ISHCMC_V_Invoice>();
            ListOfStudent = ListStudent;
            string[] ListItem = { "ADF", "APPF", "TF", "COE", "REENF" };
            List<string> ListStudentID = new List<string>();
            List<int> ListInvID = new List<int>();
            foreach (ISHCMC_V_StudentListWithDebtor itemStudent in ListStudent)
            {
                ListStudentID.Add(itemStudent.StudentID);

            }
            foreach (ISHCMC_V_Invoice itemInv in ListInvoiceDetail)
            {
                ListInvID.Add(itemInv.ID);
            }
            var queryInv = from InvDetail in DS.ISHCMC_V_Invoices

                           where ListStudentID.Contains(InvDetail.StudentID)

                           && InvDetail.Status == 0
                           && InvDetail.Fulfilled == fulfilled
                           && !ListItem.Contains(InvDetail.Itemcode)
                           && ListInvID.Contains(InvDetail.ID)
                           select InvDetail;
                  ProcessListInvoice(queryInv.ToArray());

        }
        public void LoadInvoiceDetailPrintedVAT(List<ISHCMC_V_StudentListWithDebtor> ListStudent, List<ISHCMC_V_Invoice> ListInvoiceDetail, string fulfilled)
        {
            List<ISHCMC_V_Invoice> ListInvoice = new List<ISHCMC_V_Invoice>();
            ListOfStudent = ListStudent;

            var queryExactInvoice = from ExactInvoice in DS.ISHCMC_V_ExactInvoices
                                    select ExactInvoice;
            string[] ListItem = { "ADF", "APPF", "TF", "COE", "REENF" };

            List<string> ListStudentID = new List<string>();
            List<int> ListInvID = new List<int>();
            foreach (ISHCMC_V_StudentListWithDebtor itemStudent in ListStudent)
            {
                ListStudentID.Add(itemStudent.StudentID);

            }
            foreach (ISHCMC_V_Invoice itemInv in ListInvoiceDetail)
            {
                ListInvID.Add(itemInv.ID);
            }
            var queryInv = from InvDetail in DS.ISHCMC_V_Invoices
                           join TaxInvTrack in DS.ISHCMC_TaxInvoiceTrackings
                           on InvDetail.ProformaInvoiceNo equals TaxInvTrack.InvoiceNo
                           where ListStudentID.Contains(InvDetail.StudentID)

                           && InvDetail.Status == 0 && TaxInvTrack.Status != 1
                           && InvDetail.StudentID == TaxInvTrack.StudentID
                           && InvDetail.Fulfilled == fulfilled
                            && !ListItem.Contains(InvDetail.Itemcode)
                            && ListInvID.Contains(InvDetail.ID)
                           select InvDetail;

                  ProcessListInvoice(queryInv.ToArray());

        }
        private void btnProcess_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<ISHCMC_V_Invoice> ListInvoice = new List<ISHCMC_V_Invoice>();
            foreach(int i in this.gridView_Invoice.GetSelectedRows())
            {
                ListInvoice.Add((ISHCMC_V_Invoice)this.gridView_Invoice.GetRow(i));
            }
            bool CheckProcess=false;
            string ErrorMessage ="";
            ISHCMC_V_Invoice InvoiceError = new ISHCMC_V_Invoice();
            string filename = xmlExport.ExportSalesInvoice(ListOfStudent, ListInvoice,ref CheckProcess,ref ErrorMessage,ref InvoiceError);
            if (!CheckProcess)
            {
                MessageBox.Show(ErrorMessage +" "+InvoiceError.ProformaInvoiceNo);
            }
            else
            {
                xmlExport.StartProcessImportInvoice(filename);
                foreach (ISHCMC_V_StudentListWithDebtor itemStudent in ListOfStudent)
                {
                    foreach (ISHCMC_V_Invoice InvItem in ListInvoice)
                    {
                        if (InvItem.StudentID == itemStudent.StudentID)
                        {
                            common.UpdateCostUnitForInvoiceLine(InvItem, itemStudent);
                        }
                    }
                }
                MessageBox.Show("Successfully");
            }
        }

        private void chk_RedInvoice_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_RedInvoice.Checked)
            {
                this.LoadInvoicePrintedVAT(ListOfStudent,"UnFulfilled");
            }
            else
            {
                this.LoadInvoiceWithoutDialog(ListOfStudent,"UnFulfilled");
            }
        }

        private void radioGroup_Fulfilled_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroup_Fulfilled.SelectedIndex == 0)
            {
                this.LoadInvoiceWithoutDialog(ListOfStudent, "Fulfilled");
            }
            else
            {
                this.LoadInvoiceWithoutDialog(ListOfStudent, "UnFulfilled");
            }
        }
        
    }
}